package com.starlight.flower_server.domain.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.starlight.flower_server.controller.exception.NotFoundException;
import com.starlight.flower_server.domain.TotalData;
import com.starlight.flower_server.domain.article.cqrs.ArticlePageQuery;
import com.starlight.flower_server.domain.article.data.ArticleData;
import com.starlight.flower_server.infrastructure.Status;
import com.starlight.flower_server.infrastructure.entity.Article;
import com.starlight.flower_server.infrastructure.entity.FlowerUser;
import com.starlight.flower_server.infrastructure.repository.ArticleRepository;
import com.starlight.flower_server.infrastructure.repository.CommentRepository;
import com.starlight.flower_server.infrastructure.repository.FlowerUserRepository;
import com.starlight.flower_server.domain.service.ArticleService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @Author yfy2001
 * @date 2023/4/6 8:45
 */
@Service
public class ArticleServiceImpl implements ArticleService {

    @Resource
    private ArticleRepository articleRepository;

    @Resource
    private FlowerUserRepository flowerUserRepository;

    @Resource
    private CommentRepository commentRepository;


    @Override
    public ArticleData queryArticleDetailById(Long id){
        Article article=articleRepository.queryDetail(id);
        if (article==null){
            throw new NotFoundException(Status.RECORD_NOT_FOUND.getMessage(),Status.RECORD_NOT_FOUND);
        }
        FlowerUser author=flowerUserRepository.queryDetail(article.getFlowerUserId());
        ArticleData articleData=new ArticleData();
        BeanUtils.copyProperties(article,articleData);
        articleData.setAuthor(author.getNickname());
        return articleData;
    }

    @Override
    public Integer logicDeleteArticleById(Long id) {
        Integer i= articleRepository.logicDelete(id);
        if (!i.equals(0)){
            //删除文章后移除所有评论
            commentRepository.removeAllByArticleId(id);
        }else {
            throw new NotFoundException(Status.RECORD_NOT_FOUND.getMessage(),Status.RECORD_NOT_FOUND);
        }
        return i;
    }

    @Override
    public Integer insertArticle(Article article) {
        return articleRepository.insert(article);
    }

    @Override
    public Integer modifyArticle(Article article) {
        return articleRepository.modify(article);
    }

    @Override
    public IPage<ArticleData> queryArticleDataList(ArticlePageQuery query) {
        IPage<Article> page=articleRepository.queryPage(query);
        IPage<ArticleData> data=page.convert(article -> {
            ArticleData articleData=new ArticleData();
            BeanUtils.copyProperties(article, articleData);
            return articleData;
        });
        List<ArticleData> articleDataList=data.getRecords();
        setFlowerUserInfo(articleDataList);
        data.setRecords(articleDataList);
        return data;
    }

    @Override
    public TotalData<ArticleData> queryAllArticleData() {
        TotalData<ArticleData> data=new TotalData<>();
        List<Article> articleList=articleRepository.queryAll();
        List<ArticleData> articleDataList=articleList
                .stream().map(article -> {
                    ArticleData articleData=new ArticleData();
                    BeanUtils.copyProperties(article,articleData);
                    return articleData;
                }).toList();
        setFlowerUserInfo(articleDataList);
        Long total=(long) articleDataList.size();
        data.setTable("Article");
        data.setStatTime(new Date());
        data.setTotal(total);
        data.setRecords(articleDataList);
        return data;
    }

    private void setFlowerUserInfo(List<ArticleData> data) {
        List<Long> flowerUserIdList = data.stream().map(ArticleData::getFlowerUserId).toList();
        Map<Long, FlowerUser> flowerUserMap=flowerUserRepository.queryMap(flowerUserIdList);
        for (ArticleData a:data) {
            FlowerUser flowerUser=flowerUserMap.get(a.getFlowerUserId());
            if (flowerUser!=null){
                a.setAuthor(flowerUser.getNickname());
            }else {
                a.setAuthor("此用户已经销");
            }
        }
    }

    @Override
    public TotalData<ArticleData> queryArticleTotalCount() {
        TotalData<ArticleData> data=new TotalData<>();
        Long total=articleRepository.queryTotalCount();
        data.setTable("Article");
        data.setStatTime(new Date());
        data.setTotal(total);
        return data;
    }

    @Override
    public List<ArticleData> queryRandomArticleList(Integer count) {
        List<Article> articleList=articleRepository.queryRandom(count);
        List<ArticleData> articleDataList=articleList
                .stream().map(article -> {
                    ArticleData articleData=new ArticleData();
                    BeanUtils.copyProperties(article,articleData);
                    return articleData;
                }).toList();
        setFlowerUserInfo(articleDataList);
        return articleDataList;
    }

    @Override
    public Integer addArticleHit(Long id) {
        return articleRepository.addHitByArticleId(id);
    }


}
